var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./component", "components/common/component", "components/common/base-component", "common", "react", "maishu-ui-toolkit"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const component_1 = require("./component");
    const component_2 = require("components/common/component");
    const base_component_1 = require("components/common/base-component");
    const common_1 = require("common");
    const React = require("react");
    const ui = require("maishu-ui-toolkit");
    let ShoppingCartFooter = class ShoppingCartFooter extends base_component_1.BaseComponent {
        constructor(props) {
            super(props);
            this.state = {};
        }
        get shoppingCart() {
            if (this._shoppingCart == null) {
                let c = this.masterPage.childComponents[component_1.ShoppingCart.name];
                console.assert(c.length == 1);
                this._shoppingCart = c[0];
            }
            return this._shoppingCart;
        }
        checkAll(checked) {
            let { items } = this.state;
            console.assert(items != null);
            let status = this.state.status;
            if (status == "normal") {
                items.forEach(o => o.Selected = checked);
                this.setState({ items });
                return;
            }
            if (checked)
                this.setState({ editSelectedItems: items });
            else
                this.setState({ editSelectedItems: [] });
        }
        componentDidMount() {
            setTimeout(() => {
                // 必须要延时,才能获取到 shoppingCart 控件
                common_1.asyncComponentState(this.shoppingCart, this, "status", "editSelectedItems", "items");
            }, 100);
        }
        selectedCount() {
            if (this.state.status == "normal") {
                let items = this.state.items || [];
                let count = 0;
                items.forEach(o => count + o.Count);
                return count;
            }
            return (this.state.editSelectedItems || []).length;
        }
        isSelectedAll() {
            if (this.state.items.length == 0)
                return false;
            if (this.state.status == "normal") {
                let selectedItems = this.state.items.filter(o => o.Selected);
                return selectedItems.length == this.state.items.length && this.state.items.length > 0;
            }
            return this.state.editSelectedItems.length == this.state.items.length;
        }
        render() {
            if (this.state.editSelectedItems == null || this.state.items == null)
                return React.createElement(React.Fragment, null);
            let selectedCount = this.selectedCount();
            let isCheckedAll = this.isSelectedAll();
            let totalAmount = 0;
            this.state.items.forEach(o => {
                totalAmount = totalAmount + o.Amount * o.Count;
            });
            let { status } = this.state;
            return React.createElement("div", { className: "productInfoBottomBar-control shopping-shoppingCart" },
                React.createElement("div", { className: "settlement", style: { bottom: this.props.hideMenu ? 0 : null, paddingLeft: 0 } },
                    React.createElement("div", { className: "pull-right" }, status == 'normal' ?
                        React.createElement("button", { className: "btn btn-primary", disabled: selectedCount == 0, ref: (e) => {
                                if (!e)
                                    return;
                                ui.buttonOnClick(e, () => this.shoppingCart.buy());
                            } }, selectedCount > 0 ? `结算（${selectedCount}）` : '结算')
                        :
                            React.createElement("button", { className: "btn btn-primary", disabled: selectedCount == 0, ref: (e) => {
                                    if (!e)
                                        return;
                                    ui.buttonOnClick(e, o => this.shoppingCart.removeSelectedItems(), {
                                        confirm: this.shoppingCart.removeConfirmText()
                                    });
                                } }, "\u5220\u9664")),
                    React.createElement("div", { style: { width: '100%', paddingTop: 8 } },
                        React.createElement("button", { className: "select-all pull-left", onClick: (e) => { this.checkAll(!this.isSelectedAll()); } },
                            isCheckedAll ?
                                React.createElement("i", { className: "icon-ok-sign text-primary" })
                                :
                                    React.createElement("i", { className: "icon-circle-blank text-primary" }),
                            React.createElement("span", { className: "text" }, "\u5168\u9009")),
                        this.state.status == 'normal' ?
                            React.createElement("label", { className: "pull-right", style: { paddingRight: 10, paddingTop: 2 } },
                                "\u603B\u8BA1\uFF1A",
                                React.createElement("span", { className: "price" },
                                    "\uFFE5",
                                    totalAmount.toFixed(2)))
                            : null)));
        }
    };
    ShoppingCartFooter = __decorate([
        component_2.component()
    ], ShoppingCartFooter);
    exports.ShoppingCartFooter = ShoppingCartFooter;
});
